package Saas_Exercises;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Assignment02_CountWords {

	static int countWords(String S) {
		int counter = 0;
		boolean word = false;
		int end_of_line = S.length() - 1;

		for (int i = 0; i < S.length(); i++) {
			char c = S.charAt(i);
			int I = c;

			if (((I >= 65 && I <= 90) || (I >= 97 && I <= 122) || (I >= 48 && I <= 57))
					&& i != end_of_line) {
				word = true;
			} else if (((I >= 65 && I <= 90) || (I >= 97 && I <= 122) || (I >= 48 && I <= 57)) == false
					&& word == true && (I == 32)) {
				word = false;
				counter++;
			} else if (((I >= 65 && I <= 90) || (I >= 97 && I <= 122) || (I >= 48 && I <= 57))
					&& i == end_of_line) {
				counter++;
			}
		}
		return counter;
	}

	public static void main(String[] args) throws IOException {

		System.out
				.println("Please enter your text:: After you are done entering it "
						+ "please enter stop to get the count of the words");
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String sample = br.readLine();

		int final_count = 0;

		while (!sample.equalsIgnoreCase("stop")) {

			int count = countWords(sample);
			final_count = final_count + count; // entering Crtl Z is essential
												// to confirm end of input

			sample = br.readLine();
		}
		System.out.println("final_count ::" + final_count);

	}

}
